<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form" :validatorRules="validatorRules">
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="访问密钥地址" prop="accessKeyId">
          <a-input placeholder="请输入访问密钥地址accessKeyId" v-decorator="['accessKeyId', validatorRules.accessKeyId ]" />
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="访问密钥" prop="accessSecret">
          <a-input placeholder="请输入访问密钥accessSecret" v-decorator="['accessSecret', validatorRules.accessSecret ]" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>
<script>
import { httpAction ,getAction } from '@/api/manage'
import pick from 'lodash.pick'
import moment from 'moment'
import {validatePhone,validateIdNo} from '@/utils/validator'

export default {
  name: 'SystemParameterDetails',
 
  data() {
    return {
      title: '操作',
      visible: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },

      confirmLoading: false,
      form: this.$form.createForm(this),
      validatorRules: {
        accessKeyId: { rules: [{ required: true, message: '请输入访问密钥地址accessKeyId!' }] },
        accessSecret: { rules: [{ required: true, message: '请输入访问密钥accessSecret!' }] },
        idCard: { rules: [{ required: true, message: '请输入身份证号' },{validator:validateIdNo,trigger:blur}] },
        code: { rules: [{ required: true, message: '请输入验证码' }] }
      },
      url: {
        add: '/system-api/sys/params/setMessageParams',
        getMessageParams:'/system-api/sys/params/getMessageParams'
      }
    }
  },
  created() {},
  
  methods: {
     show() {
      this.visible = true
      this.$nextTick(() => {
        getAction(this.url.getMessageParams).then(res => {
          if (res.success) {
            this.model = res.result
            this.$nextTick(() => {
            this.form.setFieldsValue(
              pick(this.model, 'accessKeyId', 'accessSecret', 'type')
            )
            //时间格式化
          })
          }
          if (res.code === 510) {
            this.$message.warning(res.message)
          }
        })
      })
    },
   
    add() {
      this.edit({})
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'post'
          }
          let formData = Object.assign(this.model, values)
          //时间格式化
          console.log(formData)
          httpAction(httpurl, formData, method)
            .then(res => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        }
      })
    },
    handleCancel() {
      this.close()
    }
  }
}
</script>

<style lang="less" scoped>
</style>